package offer;

//树的子结构
public class T18 {

     public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
     }
    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        return test(root1,root2,false);
    }
    public boolean test(TreeNode root1, TreeNode root2 ,boolean flagg) {
        if (root2==null) return  flagg;
        if (root1==null) return false;
        boolean flag = false;
        if (root1.val==root2.val) {
            flag = test(root1.left,root2.left,true)&&test(root1.right,root2.right,true);
        }
        if (flagg==false&&flag==false){
            flag = flag || test(root1.left,root2,false)||test(root1.right,root2,false);
        }
        return flag;
    }


}
